package com.orangelife.mobile.web;

import android.app.Activity;
import android.content.Context;

import com.common.sp.CommSp2;
import com.common.sp.UserSp;
import com.curry.android.util.SystemInfoUtil;
import com.orangelife.arenasdk.main.Arena;
import com.orangelife.arenasdk.main.EventCallback;
import com.orangelife.mobile.util.LogE;

/**
 * <p>
 * Created by 吴格非 on 2016-10-10.
 * <p>
 *
 * @author 吴格非
 * @since v1.0.0
 */

public class ArenaJs extends CommonJs {

    private Context mContext;

    public ArenaJs(Activity context) {
        super(context);
        this.mContext = context;
    }

    @android.webkit.JavascriptInterface
    public void arenaInit(String functionName, String seqID, String gameID) {/*初始化游戏SDK*/
        Arena.getInstance().init(functionName, seqID, gameID, UserSp.getAccessToken(), CommSp2.getCommID(), SystemInfoUtil.getVersionName(mContext), new EventCallback() {
            @Override
            public void onFinish(String functionName, String seqID, String result) {
                sendFunction(functionName, seqID, result);
            }
        });
    }

    @android.webkit.JavascriptInterface
    public void arenaEvent(String functionName, String seqID, String json) {/*提交游戏数据*/
        Arena.getInstance().event(functionName, seqID, json, new EventCallback() {
            @Override
            public void onFinish(String functionName, String seqID, String result) {
                sendFunction(functionName, seqID, result);
            }
        });
    }

    @android.webkit.JavascriptInterface
    public void arenaRank(String functionName, String seqID, String json) {/*排行榜*/
        Arena.getInstance().rank(functionName, seqID, json, new EventCallback() {
            @Override
            public void onFinish(String functionName, String seqID, String result) {
                sendFunction(functionName, seqID, result);
            }
        });
    }

    private void sendFunction(String functionName, String seqID, String result) {
        if (checkHandler()) {
            LogE.E("result = " + result);
            String js = "javascript:" + functionName + "('" + seqID + "','" + result + "')";
            mHandler.send(WebWhat.SEND_FUNCTION_JSON2, js);
        }
    }


}
